package org.sample.jdbc.commons.io;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class FileUtils {

    private static final String Separator = System.getProperty("line.separator");

    public static String read(String filePath) {
        try {
            return read(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String readIn(String classPath) {
        return read(Thread.class.getResourceAsStream(classPath));
    }

    public static String read(File file) {
        try {
            return read(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static String read(InputStream is) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
                result.append(Separator);
            }
            return result.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(reader);
        }
        return null;
    }


    public static void write(String filePath, String text) {
        try {
            write(new FileOutputStream(filePath), text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(File file, String text) {
        try {
            write(new FileOutputStream(file), text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(OutputStream os, String text) {
        BufferedWriter writer = null;
        try {
            Writer osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            writer = new BufferedWriter(osw);
            writer.write(text);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(writer);
        }
    }

    public static void copy(String sourcePath, String targetPath) {

        try (FileInputStream fis = new FileInputStream(sourcePath);
             FileOutputStream fos = new FileOutputStream(targetPath);

             FileChannel ifc = fis.getChannel();
             FileChannel ofc = fos.getChannel()) {

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            while (ifc.read(buffer) != -1) {
                buffer.flip();//切换为读模式 设置limit为position，并重置position为0
                ofc.write(buffer);
                buffer.clear();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Closeable a) {
        if (a != null)
            try {
                a.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

}